package com.vita.core2;

import java.util.Comparator;
import java.util.TreeSet;
import java.util.concurrent.ExecutionException;

/**
 * 中控
 */
public class Central {

    private Machine[] ms;

    /**
     * 上升列表，升序
     */
    private TreeSet<Integer> upList = new TreeSet<>(Integer::compareTo);
    /**
     * 下降列表，降序
     */
    private TreeSet<Integer> dwList = new TreeSet<>(Comparator.reverseOrder());

    public TreeSet<Integer> getUpList() {
        return upList;
    }

    public void setUpList(Integer... upList) throws InterruptedException, ExecutionException {
        Central central = this;
        for(int i:upList){
            Thread.sleep(100);
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Helper.upFloor(i,central);
                    } catch (InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                    }
                    
                }
                
            }).start();
            
        }
    }

    public TreeSet<Integer> getDwList() {
        return dwList;
    }

    public void setDwList(Integer... dwList) throws InterruptedException {
        Central central = this;
        for(int i:dwList){
            Thread.sleep(500);
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Helper.dwFloor(i,central);
                    } catch (InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                    }
                    
                }
                
            }).start();
            
        }
    }

    public Machine[] getMs() {
        return ms;
    }

    public void setMs(Machine[] ms) {
        this.ms = ms;
    }

    public Central(Machine... ms) {
        this.ms = ms;
        for(Machine m:this.ms){
            m.init(this);
        }
    }

    public void run(){

        for (Machine m:this.ms){
            new Thread(()-> {
                try {
                    m.run().get();
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
